<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'check:m8OperCheckDel:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="70%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>

    <Tabs v-model:activeKey="activeKey" tabPosition="left">
      <Tabs.TabPane key="1" :forceRender="true" tab="基本信息">
        <h2 style="margin-bottom: 5px">
          【 订单编号：{{ info.ccode }} 图号：{{ info.picno }} 图名：{{ info.picname }} 】
        </h2>
        <BasicForm @register="registerForm1" />
      </Tabs.TabPane>
      <Tabs.TabPane key="2" :forceRender="true" tab="原因分析">
        <BasicForm @register="registerForm2" />
      </Tabs.TabPane>
      <Tabs.TabPane key="3" :forceRender="true" tab="处理方案">
        <BasicForm @register="registerForm3" />
      </Tabs.TabPane>
      <Tabs.TabPane key="4" :forceRender="true" tab="最终处理">
        <BasicForm @register="registerForm4" />
      </Tabs.TabPane>
    </Tabs>
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsCheckM8OperCheckDelForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import {
    M8OperCheckDel,
    m8OperCheckDelSave,
    m8OperCheckDelForm,
    listTreeData,
  } from '/@/api/m8/order/m8OperCheckDel';
  import { officeTreeData } from '/@/api/sys/office';
  import { Tabs } from 'ant-design-vue';
  let activeKey = ref<string>('1');
  const emit = defineEmits(['success', 'register']);

  let info = ref<any>({
    checkId: '',
    ccode: '',
    picId: '',
    picno: '',
    picname: '',
    ope: '',
    dealStatus: '',
  });

  const { t } = useI18n('check.m8OperCheckDel');
  const { showMessage } = useMessage();
  const record = ref<M8OperCheckDel>({} as M8OperCheckDel);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: '不良品处理单',
  }));

  const [
    registerForm1,
    {
      resetFields: resetFields1,
      setFieldsValue: setFieldsValue1,
      validate: validate1,
      setProps: setProps1,
    },
  ] = useForm();
  const [
    registerForm2,
    {
      resetFields: resetFields2,
      setFieldsValue: setFieldsValue2,
      validate: validate2,
      setProps: setProps2,
    },
  ] = useForm();
  const [
    registerForm3,
    {
      resetFields: resetFields3,
      setFieldsValue: setFieldsValue3,
      validate: validate3,
      setProps: setProps3,
    },
  ] = useForm();
  const [
    registerForm4,
    {
      resetFields: resetFields4,
      setFieldsValue: setFieldsValue4,
      validate: validate4,
      setProps: setProps4,
    },
  ] = useForm();

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    info.value = data;
    setDrawerProps({ loading: true });
    const res = await m8OperCheckDelForm(data);
    record.value = (res.m8OperCheckDel || {}) as M8OperCheckDel;
    record.value.__t = new Date().getTime();
    info.value.ccode = record.value.order.ccode;
    info.value.picno = record.value.orderPic.picno;
    info.value.picname = record.value.orderPic.picname;
    info.value.picId = record.value.orderPic.id;
    info.value.dealStatus = record.value.dealStatus;
    setProps1({
      labelWidth: 120,
      schemas: [
        {
          label: t('处理单号'),
          field: 'djNo',
          component: 'Input',
          dynamicDisabled: true,
          componentProps: {
            maxlength: 64,
          },
          show: true,
        },
        {
          label: t('检验单号'),
          field: 'checkId',
          component: 'Input',
          componentProps: {
            maxlength: 64,
          },
          dynamicDisabled: true,
          show: true,
        },
        {
          label: t('责任工序'),
          field: 'operCode',
          component: 'TreeSelect',
          // dynamicDisabled: true,
          componentProps: {
            api: listTreeData,
            params: { picId: info.value.picId },
          },
          required: true,
        },
        {
          label: t('处理数量'),
          field: 'iqty',
          component: 'Input',
          dynamicDisabled: info.value.ope == 0 && info.value.dealStatus == '2' ? false : true,
          componentProps: {
            maxlength: 8,
          },
          rules: [{ required: true }, { pattern: /^\d+$/, message: t('请输入一个正整数') }],
        },
        {
          label: t('是否需要纠正预防措施'),
          field: 'isNeedYf',
          component: 'RadioGroup',
          dynamicDisabled: info.value.ope == 0 && info.value.dealStatus == '2' ? false : true,
          componentProps: {
            dictType: 'sys_yes_no',
          },
          labelWidth: 200,
        },
        {
          label: t('是否需要客户审理'),
          field: 'isNeedCus',
          component: 'RadioGroup',
          dynamicDisabled: info.value.ope == 0 && info.value.dealStatus == '2' ? false : true,
          componentProps: {
            dictType: 'sys_yes_no',
          },
          labelWidth: 200,
          colProps: { lg: 12, md: 12 },
        },
        {
          label: t('不合格描述'),
          field: 'checkRemarks',
          component: 'InputTextArea',
          dynamicDisabled: info.value.ope == 0 && info.value.dealStatus == '2' ? false : true,
          componentProps: {
            maxlength: 500,
            rows: 4,
          },
          colProps: { lg: 24, md: 24 },
          required: true,
        },
        {
          label: t('检验员'),
          field: 'checkor',
          fieldLabel: 'checkorName',
          component: 'TreeSelect',
          dynamicDisabled: info.value.ope == 0 && info.value.dealStatus == '2' ? false : true,
          componentProps: {
            api: officeTreeData,
            params: { isLoadUser: true, userIdPrefix: '', isAll: true, isShowCode: true },
            canSelectParent: false,
            allowClear: true,
          },
          colProps: { lg: 24, md: 24 },
        },
        {
          label: t('返修工时'),
          field: 'rsTime',
          component: 'Input',
          dynamicDisabled: info.value.ope == 0 && info.value.dealStatus == '2' ? false : true,
          componentProps: {
            maxlength: 8,
          },
          rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
        },
        {
          label: t('返修金额'),
          field: 'rsMoney',
          component: 'Input',
          dynamicDisabled: info.value.ope == 0 && info.value.dealStatus == '2' ? false : true,
          componentProps: {
            maxlength: 16,
          },
          rules: [
            {
              pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/,
              message: t('请输入一个数值'),
            },
          ],
        },
        {
          label: t('是否扣分'),
          field: 'isDedPoint',
          component: 'Select',
          dynamicDisabled: info.value.ope == 0 && info.value.dealStatus == '2' ? false : true,
          componentProps: {
            dictType: 'sys_yes_no',
            maxlength: 10,
          },
        },
        {
          label: t('当前阶段'),
          field: 'dealStatus',
          component: 'Select',
          dynamicDisabled: true,
          componentProps: {
            dictType: 'm8_check_deal_status',
            defaultValue: '0',
            maxlength: 10,
          },
          colProps: { lg: 24, md: 24 },
        },
        {
          label: t('附件上传'),
          field: 'dataMap',
          component: 'Upload',
          componentProps: {
            loadTime: computed(() => record.value.__t),
            bizKey: computed(() => record.value.id),
            bizType: 'm8OperCheckDeal_file',
            uploadType: 'image',
            maxSize: 100,
          },
          dynamicDisabled: info.value.ope == 0 && info.value.dealStatus == '2' ? false : true,
          colProps: { lg: 24, md: 24 },
        },
        {
          label: t('创建人'),
          field: 'creator.userName',
          component: 'Input',
          dynamicDisabled: true,
          componentProps: {
            maxlength: 64,
          },
          show: true,
        },
        {
          label: t('创建时间'),
          field: 'createDate',
          component: 'Input',
          dynamicDisabled: true,
          componentProps: {
            maxlength: 64,
          },
          show: true,
        },
      ],
      baseColProps: { lg: 12, md: 24 },
    });

    setProps2({
      labelWidth: 120,
      schemas: [
        {
          label: t('原因分析'),
          field: 'operReson',
          component: 'InputTextArea',
          dynamicDisabled: info.value.ope == 0 && info.value.dealStatus == '2' ? false : true,
          componentProps: {
            maxlength: 500,
            rows: 6,
          },
          colProps: { lg: 24, md: 24 },
        },
        {
          label: t('确认人'),
          field: 'operator',
          fieldLabel: 'operatorName',
          component: 'TreeSelect',
          dynamicDisabled: info.value.ope == 0 && info.value.dealStatus == '2' ? false : true,
          componentProps: {
            api: officeTreeData,
            params: { isLoadUser: true, userIdPrefix: '', isAll: true, isShowCode: true },
            canSelectParent: false,
            allowClear: true,
          },
        },
        {
          label: t('确认日期'),
          field: 'operatorSubDate',
          component: 'DatePicker',
          dynamicDisabled: info.value.ope == 0 && info.value.dealStatus == '2' ? false : true,
          componentProps: {
            format: 'YYYY-MM-DD HH:mm',
            showTime: { format: 'HH:mm' },
          },
          labelWidth: 180,
        },
        {
          label: t('责任人'),
          field: 'operResonCode',
          fieldLabel: 'operResonName',
          component: 'TreeSelect',
          dynamicDisabled: info.value.ope == 0 && info.value.dealStatus == '2' ? false : true,
          componentProps: {
            api: officeTreeData,
            params: { isLoadUser: true, userIdPrefix: '', isAll: true, isShowCode: true },
            canSelectParent: false,
            allowClear: true,
            treeCheckable: true,
          },
        },
      ],
      baseColProps: { lg: 12, md: 24 },
    });

    setProps3({
      labelWidth: 120,
      schemas: [
        {
          label: t('处理意见类型'),
          field: 'dealType',
          component: 'RadioGroup',
          dynamicDisabled: info.value.ope == 0 && info.value.dealStatus == '2' ? false : true,
          componentProps: {
            dictType: 'm8_deal_type',
            allowClear: true,
          },
          colProps: { lg: 24, md: 24 },
        },
        {
          label: t('最终处理方案'),
          field: 'dealRemarks',
          component: 'InputTextArea',
          dynamicDisabled: info.value.ope == 0 && info.value.dealStatus == '2' ? false : true,
          componentProps: {
            maxlength: 500,
            rows: 6,
          },
          colProps: { lg: 24, md: 24 },
        },
        {
          label: t('方案负责人'),
          field: 'resultManager',
          fieldLabel: 'resultManagerName',
          component: 'TreeSelect',
          dynamicDisabled: info.value.ope == 0 && info.value.dealStatus == '2' ? false : true,
          componentProps: {
            api: officeTreeData,
            params: { isLoadUser: true, userIdPrefix: '', isAll: true, isShowCode: true },
            canSelectParent: false,
            allowClear: true,
          },
        },
        {
          label: t('方案确认日期'),
          field: 'dealResultSunDate',
          component: 'DatePicker',
          dynamicDisabled: true,
          componentProps: {
            format: 'YYYY-MM-DD HH:mm',
            showTime: { format: 'HH:mm' },
          },
          labelWidth: 180,
        },
      ],
      baseColProps: { lg: 12, md: 24 },
    });

    setProps4({
      labelWidth: 120,
      schemas: [
        {
          label: t('最终处理意见'),
          field: 'lastDealResult',
          component: 'InputTextArea',
          dynamicDisabled: info.value.ope == 0 && info.value.dealStatus == '3' ? false : true,
          componentProps: {
            maxlength: 1000,
            rows: 6,
          },
          colProps: { lg: 24, md: 24 },
        },
        {
          label: t('最终处理结果'),
          field: 'lastSubResultType',
          component: 'RadioGroup',
          dynamicDisabled: info.value.ope == 0 && info.value.dealStatus == '3' ? false : true,
          componentProps: {
            dictType: 'M8_task_check_result',
          },
          colProps: { lg: 24, md: 24 },
        },
        {
          label: t('检验人员'),
          field: 'lastSubCheckor',
          fieldLabel: 'lastSubCheckorName',
          component: 'TreeSelect',
          dynamicDisabled: info.value.ope == 0 && info.value.dealStatus == '3' ? false : true,
          componentProps: {
            api: officeTreeData,
            params: { isLoadUser: true, userIdPrefix: '', isAll: true, isShowCode: true },
            canSelectParent: false,
            allowClear: true,
          },
        },
        {
          label: t('检验合格日期'),
          field: 'lastSubCheckDate',
          component: 'DatePicker',
          dynamicDisabled: info.value.ope == 0 && info.value.dealStatus == '3' ? false : true,
          componentProps: {
            format: 'YYYY-MM-DD HH:mm',
            showTime: { format: 'HH:mm' },
          },
        },
      ],
      baseColProps: { lg: 12, md: 24 },
    });

    await resetFields1();
    await resetFields2();
    await resetFields3();
    await resetFields4();
    setFieldsValue1(record.value);
    setFieldsValue2(record.value);
    setFieldsValue3(record.value);
    setFieldsValue4(record.value);
    setDrawerProps({ loading: false, showOkBtn: record.value.dealStatus == 4 ? false : true });
    activeKey.value = '1';
    if (info.value.ope == '1') {
      activeKey.value = '2';
    }
    if (info.value.ope == '2') {
      activeKey.value = '3';
    }
    if (info.value.ope == '3') {
      activeKey.value = '4';
    }
  });

  async function handleSubmit() {
    try {
      const data1 = await validate1();
      const data2 = await validate2();
      const data3 = await validate3();
      const data4 = await validate4();
      let data = { ...data1, ...data2, ...data3, ...data4 };
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        djNo: record.value.djNo,
      };
      data.ope = info.value.ope;
      const res = await m8OperCheckDelSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
